#-*-coding:utf-8-*-#

import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

os.chdir('PATH')
dr = pd.read_csv('Data_Extended6ac.csv', index_col=0) # Drought fastSIR simulation with factor five experiment of transmission and recovery parameter
wf = pd.read_csv('Data_Extended6bd.csv', index_col=0) # Wildfire fastSIR simulation with factor five experiment of transmission and recovery parameter

EMM = pd.read_csv('Data_Fig2a_1.csv', index_col=0)  # Drought data with negative sentiment and network information
Emp_data = EMM[112:]
Emp_data['Count'] = Emp_data['Count'] - Emp_data['Count'][112]
Emp_data = Emp_data.reset_index(drop=True)
Emp_data.Count = (Emp_data.Count - Emp_data.Count.shift()).fillna(0)

WEMM = pd.read_csv('Data_Fig2c_1.csv', index_col=0) # Wildfire data with negative sentiment and network information
WEmp_data = WEMM[70:]
WEmp_data['Count'] = WEmp_data['Count'] - WEmp_data['Count'][70]
WEmp_data = WEmp_data.reset_index(drop=True)
WEmp_data.Count = (WEmp_data.Count - WEmp_data.Count.shift()).fillna(0)

plt.rcParams['font.size'] = 14
xx = 194


"""


Extended Data4a


"""


df = pd.DataFrame()

mt_ = (dr['C_t1'] - dr['C_b']) / 20164
mm_ = (dr['C_m1'] - dr['C_m']) / 20164
mb_ = (dr['C_b1'] - dr['C_t']) / 20164

mtx_ = mt_[:xx].copy()
mtx_[0] = mt_[0]

for i in range(1, xx):
    mtx_[i] = (mtx_[i - 1] + mt_[i])

mtx_ = [mtx_[x]/(x+1)*100 if x != 0 else mt_[0] for x in range(xx)]

mmx_ = mm_[:xx].copy()
mmx_[0] = mm_[0]
for i in range(1, xx):
    mmx_[i] = (mmx_[i - 1] + mm_[i])

mmx_ = [mmx_[x]/(x+1)*100 if x != 0 else mm_[0] for x in range(xx)]

mbx_ = mb_[:xx].copy()
mbx_[0] = mb_[0]
for i in range(1, xx):
    mbx_[i] = (mbx_[i - 1] + mb_[i])

mbx_ = [mbx_[x]/(x+1)*100 if x != 0 else mb_[0] for x in range(xx)]

plt.figure(dpi=400,figsize=(6.4,6.6))
xx = 194

plt.fill_between(range(0,xx,1), dr['C_b1'][:xx]/20164*100,dr['C_t1'][:xx]/20164*100,facecolor='gray',interpolate=True
                 , alpha = 0.3)
plt.plot(range(0,xx,1), dr['C_m1'][:xx]/20164*100, color='black')
#plt.plot(range(0,xx,1), Emp_data[:xx].Count/1500*100+60, color='black',linestyle='dotted',alpha=0.5)

plt.fill_between(range(0,xx,1), dr['C_b'][:xx]/20164*100+60,dr['C_t'][:xx]/20164*100+60,facecolor='gray',interpolate=True
                 , alpha = 0.3)
plt.plot(range(0,xx,1), dr['C_m'][:xx]/20164*100+60, color='black')


plt.axhline(y=0,color='black',linestyle='-',linewidth=2)
plt.axhline(y=10,color='black',linestyle='--',linewidth=1)
plt.axhline(y=20,color='black',linestyle='--',linewidth=1)
plt.axhline(y=30,color='black',linestyle='--',linewidth=1)
plt.axhline(y=40,color='black',linestyle='--',linewidth=1)
plt.axhline(y=50,color='black',linestyle='--',linewidth=1)

plt.axhline(y=60,color='black',linestyle='-',linewidth=2)
plt.axhline(y=70,color='black',linestyle='--',linewidth=1)
plt.axhline(y=80,color='black',linestyle='--',linewidth=1)
plt.axhline(y=90,color='black',linestyle='--',linewidth=1)
plt.axhline(y=100,color='black',linestyle='--',linewidth=1)
plt.axhline(y=110,color='black',linestyle='--',linewidth=1)
plt.yticks([0,10,20,30,40,50,60,70,80,90,100,110],
           ['0','10','20','30','40','50','0','10','20','30','40','50'])
plt.xticks(range(0,xx,45))
plt.xlim([0,xx-1])
plt.ylim([-10,120])
plt.show()


plt.figure(dpi=400,figsize=(7,4.6))
plt.plot(range(0,xx,1), Emp_data[:xx].Count/1800*100, color='black',linestyle='dotted',alpha=0.5)
plt.fill_between(range(0,xx,1), dr['C_b'][:xx]/20164*100,dr['C_t'][:xx]/20164*100,facecolor='gray',interpolate=True
                 , alpha = 0.3)
plt.plot(range(0,xx,1), dr['C_m'][:xx]/20164*100, color='black')
plt.axhline(y=0,color='black',linestyle='-',linewidth=1)
plt.xticks(range(0,xx,45))
plt.xlim([0,xx-1])
plt.ylim([-9,50])
plt.show()



"""


Extended Data4b


"""



df = pd.DataFrame()

mt_ = (wf['C_t1'] - wf['C_b']) / 4592
mm_ = (wf['C_m1'] - wf['C_m']) / 4592
mb_ = (wf['C_b1'] - wf['C_t']) / 4592

mtx_ = mt_[:xx].copy()
mtx_[0] = mt_[0]

for i in range(1, xx):
    mtx_[i] = (mtx_[i - 1] + mt_[i])

mtx_ = [mtx_[x]/(x+1)*100 if x != 0 else mt_[0] for x in range(xx)]

mmx_ = mm_[:xx].copy()
mmx_[0] = mm_[0]
for i in range(1, xx):
    mmx_[i] = (mmx_[i - 1] + mm_[i])

mmx_ = [mmx_[x]/(x+1)*100 if x != 0 else mm_[0] for x in range(xx)]

mbx_ = mb_[:xx].copy()
mbx_[0] = mb_[0]
for i in range(1, xx):
    mbx_[i] = (mbx_[i - 1] + mb_[i])

mbx_ = [mbx_[x]/(x+1)*100 if x != 0 else mb_[0] for x in range(xx)]

plt.figure(dpi=400,figsize=(6.4,6.6))
xx = 194

plt.fill_between(range(0,xx,1), wf['C_b1'][:xx]/4592*100,wf['C_t1'][:xx]/4592*100,facecolor='gray',interpolate=True
                 , alpha = 0.3)
plt.plot(range(0,xx,1), wf['C_m1'][:xx]/4592*100, color='black')

plt.fill_between(range(0,xx,1), wf['C_b'][:xx]/4592*100+60,wf['C_t'][:xx]/4592*100+60,facecolor='gray',interpolate=True
                 , alpha = 0.3)
plt.plot(range(0,xx,1), wf['C_m'][:xx]/4592*100+60, color='black')

plt.axhline(y=0,color='black',linestyle='-',linewidth=2)
plt.axhline(y=10,color='black',linestyle='--',linewidth=1)
plt.axhline(y=20,color='black',linestyle='--',linewidth=1)
plt.axhline(y=30,color='black',linestyle='--',linewidth=1)
plt.axhline(y=40,color='black',linestyle='--',linewidth=1)
plt.axhline(y=50,color='black',linestyle='--',linewidth=1)

plt.axhline(y=60,color='black',linestyle='-',linewidth=2)
plt.axhline(y=70,color='black',linestyle='--',linewidth=1)
plt.axhline(y=80,color='black',linestyle='--',linewidth=1)
plt.axhline(y=90,color='black',linestyle='--',linewidth=1)
plt.axhline(y=100,color='black',linestyle='--',linewidth=1)
plt.axhline(y=110,color='black',linestyle='--',linewidth=1)

plt.yticks([0,10,20,30,40,50,60,70,80,90,100,110],
           ['0','10','20','30','40','50','0','10','20','30','40','50'])
plt.xticks(range(0,xx,45))
plt.xlim([0,xx-1])
plt.ylim([-10,120])
plt.show()

plt.figure(dpi=400,figsize=(7,4.6))
plt.plot(range(0,xx,1), WEmp_data[:xx].Count/600*100, color='black',linestyle='dotted',alpha=0.5)
plt.fill_between(range(0,xx,1), wf['C_b'][:xx]/4592*100,wf['C_t'][:xx]/4592*100,facecolor='gray',interpolate=True
                 , alpha = 0.3)
plt.plot(range(0,xx,1), wf['C_m'][:xx]/4592*100, color='black')
plt.axhline(y=0,color='black',linestyle='-',linewidth=1)
plt.xticks(range(0,xx,45))
plt.xlim([0,xx-1])
plt.ylim([-9,50])
plt.show()

"""


Extended Data4c


"""



plt.figure(dpi=400,figsize=(7,4.6))
plt.plot(range(0,xx,1), Emp_data[:xx].Count/1800*100, color='black',linestyle='dotted',alpha=0.5)
plt.fill_between(range(0,xx,1), dr['C_b'][:xx]/20164*100,dr['C_t'][:xx]/20164*100,facecolor='gray',interpolate=True
                 , alpha = 0.3)
plt.plot(range(0,xx,1), dr['C_m'][:xx]/20164*100, color='black')
plt.axhline(y=0,color='black',linestyle='-',linewidth=2)
plt.axhline(y=10,color='black',linestyle='--',linewidth=1)
plt.axhline(y=20,color='black',linestyle='--',linewidth=1)
plt.axhline(y=30,color='black',linestyle='--',linewidth=1)
plt.axhline(y=40,color='black',linestyle='--',linewidth=1)
plt.axhline(y=50,color='black',linestyle='--',linewidth=1)
plt.axhline(y=0,color='black',linestyle='-',linewidth=1)
plt.xticks(range(0,xx,45))
plt.xlim([0,xx-1])
plt.ylim([-9,50])
plt.show()
plt.figure(dpi=400,figsize=(6.4,3.3))
plt.fill_between(range(0,xx,1), list(map(lambda x: x , mtx_[:xx])),list(map(lambda x: x , mbx_[:xx])),facecolor='black',interpolate=True
                 , alpha = 0.2)
plt.plot(range(0,xx,1), list(map(lambda x: x , mmx_[:xx])), color='black')
plt.plot([mmx_.index(max(mmx_)),mmx_.index(max(mmx_))], [mtx_[mmx_.index(max(mmx_))],mbx_[mmx_.index(max(mmx_))]], marker='_',markersize=10,color='black')

plt.axhline(y=0,color='black',linestyle='-',linewidth=1)
plt.axhline(y=0,color='black',linestyle='-',linewidth=2)
plt.axhline(y=10,color='black',linestyle='--',linewidth=1)
plt.axhline(y=20,color='black',linestyle='--',linewidth=1)
plt.axhline(y=30,color='black',linestyle='--',linewidth=1)
plt.axhline(y=40,color='black',linestyle='--',linewidth=1)
plt.axhline(y=50,color='black',linestyle='--',linewidth=1)
plt.yticks([0,10,20,30,40,50],
           ['0','10','20','30','40','50'])
plt.xticks(range(0,xx,45))
plt.xlim([0,xx-1])
plt.ylim([-10,60])
plt.show()



"""


Extended Data4d


"""

plt.figure(dpi=400,figsize=(6.4,3.3))

plt.fill_between(range(0,xx,1), list(map(lambda x: x , mtx_[:xx])),list(map(lambda x: x , mbx_[:xx])),facecolor='black',interpolate=True
                 , alpha = 0.2)
plt.plot(range(0,xx,1), list(map(lambda x: x , mmx_[:xx])), color='black')
plt.plot([mmx_.index(max(mmx_)),mmx_.index(max(mmx_))], [mtx_[mmx_.index(max(mmx_))],mbx_[mmx_.index(max(mmx_))]], marker='_',markersize=10,color='black')

plt.axhline(y=0,color='black',linestyle='-',linewidth=1)
plt.yticks([0,10,20,30,40,50],
           ['0','10','20','30','40','50'])
plt.axhline(y=0,color='black',linestyle='-',linewidth=2)
plt.axhline(y=10,color='black',linestyle='--',linewidth=1)
plt.axhline(y=20,color='black',linestyle='--',linewidth=1)
plt.axhline(y=30,color='black',linestyle='--',linewidth=1)
plt.axhline(y=40,color='black',linestyle='--',linewidth=1)
plt.axhline(y=50,color='black',linestyle='--',linewidth=1)

plt.xticks(range(0,xx,45))
plt.xlim([0,xx-1])
plt.ylim([-10,60])
plt.show()
